package algorithm;

import data.ListNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @ClassName: ListRandomNode
 * @Description TODO
 * @Author skywingking
 * @Date 2022/1/16 10:57 下午
 **/
public class ListRandomNode {
    List<Integer> list;
    Random random;
    public ListRandomNode(ListNode head){
        list = new ArrayList<>();
        while(head != null){
            list.add(head.val);
            head = head.next;
        }
        random = new Random();
    }

    public int getRandom(){
        return list.get(random.nextInt(list.size()));
    }
}